package com.zj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zj.bean.ReturnInfo;
import com.zj.bean.User;
import com.zj.constants.Default;
import com.zj.constants.ReturnCode;
import com.zj.service.LoginService;

import cn.hn.java.summer.exception.BusinessException;

/**
 * Created by xw2sy on 2017-04-16.
 */
@Controller
@RequestMapping("/")
public class MainController extends BaseController{

	@Autowired
	LoginService loginService;
    @RequestMapping(value = "login",method=RequestMethod.GET)
    public void getLogin(User user){
    	System.out.println(user);
    }
    @RequestMapping(value = "login",method=RequestMethod.POST)
    public ReturnInfo login(User user,RedirectAttributes attr) throws BusinessException{
    	user = loginService.login(user);
    	ReturnInfo rinfo = new ReturnInfo();
    	if(user == null){
    		rinfo.setCode(ReturnCode.LOGIN_FAIL);
    		rinfo.setMsg("用户名或密码错误");
    		return rinfo;
    	}
		addSessionAttribute(Default.USER_SESSION_KEY,user);
		rinfo.setMsg("用户登录成功");
		return rinfo;
    }

    @RequestMapping("/index")
    public void index() throws BusinessException{
    	System.out.println("hehe");
    }
    /**
	 * 退出登录
	 */
	@RequestMapping("/logout")
	public void logout(){
		clearSession();
		redirectTo("/login.html");
	}
}
